package com.binfei.yjs.server.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AnnSwaggerConfig {

    @Bean
    OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("API接口文档")
                        .summary("API后端接口文档")
                )
                // 配置全局鉴权参数-Authorize
                .components(
                        new Components()
                                .addSecuritySchemes(
                                        "X-Token",
                                        new SecurityScheme()
                                                .name("X-Token")
                                                .type(SecurityScheme.Type.APIKEY)
                                                .in(SecurityScheme.In.HEADER)
                                                .description("JWT Token Header传参")
                                )
                );
    }

    @Bean
    GlobalOpenApiCustomizer globalOpenAPICustomizer() {
        return openApi -> {
            if (openApi.getPaths() != null) {
                openApi.getPaths().forEach((k, pathItem) -> {
                    pathItem.readOperations().forEach(operation -> {
                                operation.addSecurityItem(new SecurityRequirement().addList("X-Token"));
                            }
                    );
                });
            }
        };
    }

}
